import {
	defineStore
} from 'pinia'
import * as API from "../api/api.js"
export const useCommonStore = defineStore('useCommon', {
	state: () => {
		return {
      userInfo: null,
      extraData: null,
      path: null,
		}
	},
	actions: {
    gotoMiniProgram(appInfo){
      console.log("import.meta.env.VITE_ENVVERSION", import.meta.env.VITE_ENVVERSION, appInfo)
      if(!appInfo) return
      uni.navigateToMiniProgram({
        appId: appInfo.appId,  // 被跳转的appId
        path: this.path || null,  // 要跳转的目标小程序（B小程序）的路径
        extraData: appInfo.extraData || {},
        envVersion: import.meta.env.VITE_ENVVERSION,  // 跳转的版本：develop（开发版），trial（体验版），release（正式版）
        success(res) {
          console.log("跳转成功", res)
        },
        fail(res) {
          console.log("跳转失败", res)
        }
      })
    },
    getBizParam() {
      this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : null
      return API.getBizParam(this.userInfo.customers.customerId).then(res => {
        if (res.code === 0) {
          this.extraData = res.data
          res.data.route = res.data.route || encodeURIComponent("/pages/home/index")
          this.path = `/pages/middle/activity?url=${res.data.route}&biz=${this.extraData.biz}`
          console.log("this.path", this.path)
        }
      }).catch(err => {

      })
    }
  }
})

